package cn.com.nxt.txb;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.Call;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import cn.com.nxt.txb.avchat.AVChatSoundPlayer;
import cn.com.nxt.txb.httpdelegate.LiveDelegate;
import cn.com.nxt.txb.util.AppManager;
import cn.com.nxt.txb.util.AppUtil;

import static cn.com.nxt.txb.avchat.AVChatSoundPlayer.RingerTypeEnum.RING;
//邀请呼叫页面，暂停使用
@ContentView(R.layout.activity_call_meeting)
public class CallMeetingActivity extends BaseActivity {
    @ViewInject(R.id.viewpager)
    private TextView mUsernameTv;
    private String mUsername;
    private String mPhone;
    private String mRoomID;
    private String dstUsername;
    private String dstPhone;
    private Timer timer;
    private LiveDelegate liveDelegate;
    private byte LIVE_ADDRESS = 0x23;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//注解绑定
        mUsername = getIntent().getStringExtra("username");
        mPhone = getIntent().getStringExtra("phone");
        mRoomID = getIntent().getStringExtra("roomid");
        dstUsername = getIntent().getStringExtra("dst_name");
        dstPhone = getIntent().getStringExtra("dst_phone");
        mUsernameTv.setText(mUsername);
        timer = new Timer();
        timer.schedule(task, 30000);
        AVChatSoundPlayer.instance().play(RING);
        liveDelegate = new LiveDelegate(this);
        bindService(new Intent(CallMeetingActivity.this, MQTTService.class), this, BIND_AUTO_CREATE);
    }

    private void issue(Integer status) {
        Map<String, Object> map = new HashMap<>();
        map.put("src_name", mUsername);
        map.put("src_phone", mPhone);
        map.put("dst_name", dstUsername);
        map.put("dst_phone", dstPhone);
        map.put("status", status);
        Gson gson = new Gson();
        if(mqttBinder==null||mqttBinder.isClientEmpty()){
            return;
        }
        //mqttBinder.publish("/meetting/invlite/resp/"+ mRoomID,gson.toJson(map));//会议邀请回复，客户端暂时不实现
    }

    @Event({R.id.iv_call_answer, R.id.iv_call_hang_up})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.iv_call_answer:
                callAnswer();
                break;
            case R.id.iv_call_hang_up:
                callHangUp();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AVChatSoundPlayer.instance().stop();
        timer.cancel();
        unbindService(this);
    }

    private void callHangUp() {
        AVChatSoundPlayer.instance().stop();
        if (AppManager.getAppManager().getActSize() > 1)
            finish();
        else {
            startActivity(new Intent(CallMeetingActivity.this, MainActivity.class));
            finish();
        }
        issue(0);
    }

    private void callAnswer() {
        AVChatSoundPlayer.instance().stop();
        showDialog("正在进入房间");
        liveDelegate.liveAddress(LIVE_ADDRESS);
        issue(1);
    }

    public void back(View v) {
        if (AppManager.getAppManager().getActSize() > 1)
            finish();
        else {
            startActivity(new Intent(CallMeetingActivity.this, MainActivity.class));
            finish();
        }
    }

    TimerTask task = new TimerTask() {
        @Override
        public void run() {

        }
    };

    @Override
    public void success(String result, byte flag) {
        super.success(result, flag);
        Intent intent = new Intent(CallMeetingActivity.this, TextureVideoActivity.class);
        intent.putExtra("roomid",mRoomID);
        intent.putExtra("path", "rtmp://" + result + ":1935/txb/" + mRoomID);
        startActivity(intent);
        finish();
    }

    /**
     * 再按一次退出系統
     */
    private long exitTime = 0;

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                showToast("再按一次挂断");
                exitTime = System.currentTimeMillis();
            } else {
                callHangUp();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
